<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="园区" prop="parkId">
        <el-select clearable v-model="form.parkId" @change="getBuildingList(form.parkId)" placeholder="请选择园区">
          <el-option
            v-for="item in buiParkList"
            :key="item.id"
            :label="item.parkNum"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="容器名称" prop="vesselName">
        <el-input
          v-model="queryParams.vesselName"
          placeholder="请输入容器名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属企业" prop="vesselCompany">
        <el-input
          v-model="queryParams.vesselCompany"
          placeholder="请输入所属企业"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="主体检验时间">
        <el-date-picker
          v-model="daterangeSubjectDate"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="仪表检验时间">
        <el-date-picker
          v-model="daterangeInstrumentDate"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="安全阀检验时间">
        <el-date-picker
          v-model="daterangeSecurityDate"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in stateOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['security:afVessel:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['security:afVessel:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['security:afVessel:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['security:afVessel:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="afVesselList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="容器名称" align="center" prop="vesselName"/>
      <el-table-column label="容器数量" align="center" prop="vesselNum"/>
      <el-table-column label="所属企业" align="center" prop="vesselCompany"/>
      <el-table-column label="联系电话" align="center" prop="phone"/>
      <el-table-column label="设备厂家" align="center" prop="vesselFactory"/>
      <el-table-column label="容积" align="center" prop="vesselVolume"/>
      <el-table-column label="工作压力" align="center" prop="workPressure"/>
      <el-table-column label="设计压力" align="center" prop="designPressure"/>
      <el-table-column label="设计温度" align="center" prop="designTemp"/>
      <el-table-column label="主体检验时间" align="center" prop="subjectDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.subjectDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="仪表检验时间" align="center" prop="instrumentDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.instrumentDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="安全阀检验时间" align="center" prop="securityDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.securityDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="state" :formatter="stateFormat"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['security:afVessel:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['security:afVessel:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改压力容器台账对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="容器名称" prop="vesselName">
          <el-input v-model="form.vesselName" placeholder="请输入容器名称"/>
        </el-form-item>
        <el-form-item label="容器数量" prop="vesselNum">
          <el-input v-model="form.vesselNum" placeholder="请输入容器数量"/>
        </el-form-item>
        <el-form-item label="所属企业" prop="vesselCompany">
          <el-input v-model="form.vesselCompany" placeholder="请输入所属企业"/>
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系电话"/>
        </el-form-item>
        <el-form-item label="设备厂家" prop="vesselFactory">
          <el-input v-model="form.vesselFactory" placeholder="请输入设备厂家"/>
        </el-form-item>
        <el-form-item label="容积" prop="vesselVolume">
          <el-input v-model="form.vesselVolume" placeholder="请输入容积"/>
        </el-form-item>
        <el-form-item label="工作压力" prop="workPressure">
          <el-input v-model="form.workPressure" placeholder="请输入工作压力"/>
        </el-form-item>
        <el-form-item label="设计压力" prop="designPressure">
          <el-input v-model="form.designPressure" placeholder="请输入设计压力"/>
        </el-form-item>
        <el-form-item label="设计温度" prop="designTemp">
          <el-input v-model="form.designTemp" placeholder="请输入设计温度"/>
        </el-form-item>
        <el-form-item label="主体检验时间" prop="subjectDate">
          <el-date-picker clearable size="small"
                          v-model="form.subjectDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择主体检验时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="仪表检验时间" prop="instrumentDate">
          <el-date-picker clearable size="small"
                          v-model="form.instrumentDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择仪表检验时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="安全阀检验时间" prop="securityDate">
          <el-date-picker clearable size="small"
                          v-model="form.securityDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择安全阀检验时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-select v-model="form.state" placeholder="请选择状态">
            <el-option
              v-for="dict in stateOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAfVessel,
  getAfVessel,
  delAfVessel,
  addAfVessel,
  updateAfVessel
} from '@/api/smartParks/security/securityVessel'
import { listBuiPark } from '@/api/parkAssets/enterCommunity'

export default {
  name: 'AfVessel',
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 压力容器台账表格数据
      buiParkList: [],
      afVesselList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 主体检验时间时间范围
      daterangeSubjectDate: [],
      // 仪表检验时间时间范围
      daterangeInstrumentDate: [],
      // 安全阀检验时间时间范围
      daterangeSecurityDate: [],
      // 状态字典
      stateOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        vesselName: null,
        vesselCompany: null,
        vesselFactory: null,
        subjectDate: null,
        instrumentDate: null,
        securityDate: null,
        state: null,
        parkId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        parkId: [
          { required: true, message: '园区不能为空', trigger: 'blur' }
        ],
        vesselCompany: [
          { required: true, message: '企业名称不能为空', trigger: 'blur' }
        ],
        vesselName: [
          { required: true, message: '容器名称', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
    this.getDicts('device_work_state').then(response => {
      this.stateOptions = response.data
    }),
      listBuiPark().then((response) => {
        this.buiParkList = response.rows
      })
  },
  methods: {
    /** 查询压力容器台账列表 */
    getList() {
      this.loading = true
      this.queryParams.params = {}
      if (null != this.daterangeSubjectDate && '' != this.daterangeSubjectDate) {
        this.queryParams.params['beginSubjectDate'] = this.daterangeSubjectDate[0]
        this.queryParams.params['endSubjectDate'] = this.daterangeSubjectDate[1]
      }
      if (null != this.daterangeInstrumentDate && '' != this.daterangeInstrumentDate) {
        this.queryParams.params['beginInstrumentDate'] = this.daterangeInstrumentDate[0]
        this.queryParams.params['endInstrumentDate'] = this.daterangeInstrumentDate[1]
      }
      if (null != this.daterangeSecurityDate && '' != this.daterangeSecurityDate) {
        this.queryParams.params['beginSecurityDate'] = this.daterangeSecurityDate[0]
        this.queryParams.params['endSecurityDate'] = this.daterangeSecurityDate[1]
      }
      listAfVessel(this.queryParams).then(response => {
        this.afVesselList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 状态字典翻译
    stateFormat(row, column) {
      return this.selectDictLabel(this.stateOptions, row.state)
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        vesselName: null,
        vesselNum: null,
        vesselCompany: null,
        phone: null,
        vesselFactory: null,
        vesselVolume: null,
        workPressure: null,
        designPressure: null,
        designTemp: null,
        subjectDate: null,
        instrumentDate: null,
        securityDate: null,
        state: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null,
        tenantId: null,
        parkId: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeSubjectDate = []
      this.daterangeInstrumentDate = []
      this.daterangeSecurityDate = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加压力容器台账'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getAfVessel(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改压力容器台账'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAfVessel(this.form).then(response => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addAfVessel(this.form).then(response => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除压力容器台账编号为"' + ids + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delAfVessel(ids)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('security/afVessel/export', {
        ...this.queryParams
      }, `security_afVessel.xlsx`)
    }
  }
}
</script>
